/*
 * 保存公司信息交易
 * 参数   交易码：trancode=14113
 * 
 */

package org.ccams.tran;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TCompany;
import org.json.JSONArray;
/**
 * 保存公司信息交易
 * @author liuguangshuai@gmail.com
 * @系统名称：定制化辅助生成制造系统
 * @时间：Aug 22, 2008
 */
public class Tran14113 extends AbstractJsonTran {

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String params = (String) request.getParameter("Company");
		JSONArray Companys = new JSONArray(params);
		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		JSONArray newCompanys = new JSONArray();
		int k = 0;
		for(int i=0;i<Companys.length();i++){
			org.json.JSONObject company = Companys.optJSONObject(i);
			if(company.has("idtCompany")){
				TCompany t = new TCompany();
				t.setValid(0);
				t.setBengEndFin(company.getString("bengEndFin"));
				t.setCompanyName(company.getString("companyName"));
				t.setRegAddress(company.getString("regAddress"));
				t.setSmallName(company.getString("smallName"));
				t.setIdtCompany(company.getLong("idtCompany"));
				k = getCcamsService().getTcompanyDao().updateByPrimaryKeySelective(t);	//更新			
			}else{
				newCompanys.put(company);
			}			
		}
		for(int i=0;i<newCompanys.length();i++){
			org.json.JSONObject newCompany = Companys.optJSONObject(i);
			TCompany t = new TCompany();
			t.setValid(0);
			t.setBengEndFin(newCompany.getString("bengEndFin"));
			t.setCompanyName(newCompany.getString("companyName"));
			t.setRegAddress(newCompany.getString("regAddress"));
			t.setSmallName(newCompany.getString("smallName"));
			k = getCcamsService().getTcompanyDao().insert(t);
		}
		if (k == 1) {
			success = true;
			message = "公司保存成功";
		} else {
			success = false;
			message = "公司保存失败";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
